package com.lsa.letcode.the114.flattenBtToLinkedList;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.google.common.primitives.Ints;
import com.lsa.helpers.Utils;
import com.lsa.letcode.common.LetCodeTestBase;
import com.lsa.letcode.common.TreeNode;
import com.lsa.letcode.common.TreeUtils;

public class FlattenBtToLinkedList extends LetCodeTestBase {
	@Test
	public void test() throws Exception {
		test("1.tgf", Ints.asList(Utils.createIntArray1D(1, 1, 1)));
		test("complete1.tgf", Ints.asList(Utils.createIntArray1D(1, 7, 1)));
		test("complete2.tgf", Ints.asList(Utils.createIntArray1D(1, 6, 1)));
		test("complete3.tgf", Ints.asList(Utils.createIntArray1D(1, 5, 1)));
//		Node<Integer> t = TreeUtils.copyAdaptTree(n);
//		System.out.println(BinaryTreeUtils.toString(t));
//		Node<Integer> t2 = TreeUtils.copyAdaptTree(sortedArrayToBST);
//		System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
//		System.out.println(String.format("<<<< %s >>>>", TreeUtils.maxDepthRec(bstFromList)));
//		System.out.println(BinaryTreeUtils.toString(t1));
//		File f = new File("data/iview/com/lsa/letcode/the114/flattenBtToLinkedList/complete7lst.tgf");
//		System.out.println(f.getAbsolutePath());
//		TreeUtils.toTgf(n, f);
	}

	private void test(String resName, List<Integer> expected)
			throws IOException {
		Solution s = new Solution();
		TreeNode n = TreeUtils.fromTgf(this.getClass(), resName);
		s.flatten(n);
		assertEquals(expected, asList(n));
	}
	
	private List<Integer> asList(TreeNode n) {
		List<Integer> l = new ArrayList<Integer>();
		while(n != null){
			if (n.left != null){
				throw new RuntimeException();
			}
			l.add(n.val);
			n = n.right;
		}
		
		return l;
	}
}
